//JETNUKE

/*
 * Copyright (c) 2004-2006 by Andrea Mazzolini [andreamazzolini@users.sourceforge.net]
 * http://www.jetnuke.org
 * 
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA.
 *  
 */

package org.jetnuke.dao.generic;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.BlogPost;

public class GenericBlogPostDAO extends AutoKeys implements
		org.jetnuke.dao.BlogPostDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(GenericBlogPostDAO.class);

	public BlogPost retrieveByPK(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BlogPostModel.selectByID"));
		p.setInt(1, id);

		ResultSet rs = p.executeQuery();
		BlogPost s = new BlogPost();

		if (rs.next()) {
			fillBlogPostFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByPK(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	protected void fillBlogPostFromResultSet(BlogPost s, ResultSet rs)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger
					.debug("fillBlogPostFromResultSet(BlogPost, ResultSet) - start"); //$NON-NLS-1$
		}

		s.setAID(rs.getInt("POST_AUTHOR"));
		s.setCOMMENTSTATUS(rs.getString("COMMENT_STATUS"));
		s.setGUID(rs.getString("GUID"));
		s.setID(rs.getInt("ID"));
		s.setPINGED(rs.getString("PINGED"));
		s.setPINGSTATUS(rs.getString("PING_STATUS"));
		s.setPOSTCATEGORY(rs.getInt("POST_CATEGORY"));
		s.setPOSTCONTENT(rs.getString("POST_CONTENT"));
		s.setPOSTDATE(rs.getTimestamp("POST_DATE"));
		s.setPOSTEXCERPT(rs.getString("POST_EXCERPT"));
		s.setPOSTMODIFIED(rs.getTimestamp("POST_MODIFIED"));
		s.setPOSTPASSWORD(rs.getString("POST_PASSWORD"));
		s.setPOSTSTATUS(rs.getString("POST_STATUS"));
		s.setPOSTTITLE(rs.getString("POST_TITLE"));
		s.setTOPING(rs.getString("TO_PING"));

		if (logger.isDebugEnabled()) {
			logger
					.debug("fillBlogPostFromResultSet(BlogPost, ResultSet) - end"); //$NON-NLS-1$
		}
	}

	public void update(BlogPost bp) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("update(BlogPost) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(SystemGlobals.getSql("BlogPostModel.update"));
		long time = new GregorianCalendar().getTime().getTime();
		p.setInt(1, bp.getAID());
		p.setTimestamp(2, new Timestamp(bp.getPOSTDATE().getTime()));
		p.setString(3, bp.getPOSTCONTENT());
		p.setString(4, bp.getPOSTTITLE());
		p.setInt(5, bp.getPOSTCATEGORY());
		p.setString(6, bp.getPOSTEXCERPT());
		p.setString(7, bp.getPOSTSTATUS());
		p.setString(8, bp.getCOMMENTSTATUS());
		p.setString(9, bp.getPINGSTATUS());
		p.setString(10, bp.getPOSTPASSWORD());
		p.setString(11, bp.getTOPING());
		p.setString(12, bp.getPINGED());
		p.setTimestamp(13, new Timestamp(time));
		p.setString(14, bp.getGUID());
		p.setInt(15, bp.getID());
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("update(BlogPost) - end"); //$NON-NLS-1$
		}
	}

	public int addNew(BlogPost bp) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("addNew(BlogPost) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = this
				.getStatementForAutoKeys("BlogPostModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals
				.getSql("BlogPostModel.lastGeneratedBlogPostID"));
		initBlogPost(bp, p);

		int id = this.executeAutoKeysQuery(p);
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("addNew(BlogPost) - end"); //$NON-NLS-1$
		}
		return id;
	}

	protected void initBlogPost(BlogPost bp, PreparedStatement p)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("initBlogPost(BlogPost, PreparedStatement) - start"); //$NON-NLS-1$
		}

		long time = new GregorianCalendar().getTime().getTime();
		p.setInt(1, bp.getAID());
		p.setTimestamp(2, new Timestamp(time));
		p.setString(3, bp.getPOSTCONTENT());
		p.setString(4, bp.getPOSTTITLE());
		p.setInt(5, bp.getPOSTCATEGORY());
		p.setString(6, bp.getPOSTEXCERPT());
		p.setString(7, bp.getPOSTSTATUS());
		p.setString(8, bp.getCOMMENTSTATUS());
		p.setString(9, bp.getPINGSTATUS());
		p.setString(10, bp.getPOSTPASSWORD());
		p.setString(11, bp.getTOPING());
		p.setString(12, bp.getPINGED());
		p.setTimestamp(13, new Timestamp(time));
		p.setString(14, bp.getGUID());

		if (logger.isDebugEnabled()) {
			logger.debug("initBlogPost(BlogPost, PreparedStatement) - end"); //$NON-NLS-1$
		}
	}

	public List retrieveByUser(int aid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByUser(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BlogPostModel.selectByUser"));
		p.setInt(1, aid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByUser(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List retrieveByUserOrdered(int aid) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByUserOrdered(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("BlogPostModel.selectByUserOrdered"));
		p.setInt(1, aid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByUserOrdered(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List retrieveByUserCat(int aid, int cat_id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByUserCat(int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BlogPostModel.selectByUserCat"));
		p.setInt(1, aid);
		p.setInt(2, cat_id);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveByUserCat(int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	protected List processSelectAll(ResultSet rs) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - start"); //$NON-NLS-1$
		}

		List list = new ArrayList();
		while (rs.next()) {
			BlogPost u = new BlogPost();
			fillBlogPostFromResultSet(u, rs);
			list.add(u);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("processSelectAll(ResultSet) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List retrieveLast(int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveLast(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BlogPostModel.selectLast"));
		p.setInt(1, limit);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveLast(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List retrievePublishByUser(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrievePublishByUser(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("BlogPostModel.selectPublishByUser"));
		p.setInt(1, id);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrievePublishByUser(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List retrievePublishByUserCat(int id, int cat) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrievePublishByUserCat(int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("BlogPostModel.selectPublishByUserCat"));
		p.setInt(1, id);
		p.setInt(2, cat);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrievePublishByUserCat(int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List retrievePublishByUser(int id, int maxnumberpost)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrievePublishByUser(int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("BlogPostModel.selectPublishByUserLimit"));
		p.setInt(1, id);
		p.setInt(2, maxnumberpost);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrievePublishByUser(int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List retrieveDraftByUser(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrieveDraftByUser(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BlogPostModel.selectDraftByUser"));
		p.setInt(1, id);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrieveDraftByUser(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List retrievePrivateByUser(int id) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrievePrivateByUser(int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("BlogPostModel.selectPrivateByUser"));
		p.setInt(1, id);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrievePrivateByUser(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public void changeCategory(int oldCat, int newCat) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("changeCategory(int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BlogPostModel.changeCategory"));
		p.setInt(1, newCat);
		p.setInt(2, oldCat);
		p.executeUpdate();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("changeCategory(int, int) - end"); //$NON-NLS-1$
		}
	}

	public Set getPostDays(int aid, int month, int year) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getPostDays(int, int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(
						SystemGlobals.getSql("BlogPostModel.selectPostDays"));
		p.setInt(1, aid);
		p.setInt(2, month);
		p.setInt(3, year);
		ResultSet rs = p.executeQuery();
		Set set = new HashSet();
		while (rs.next()) {
			set.add(rs.getString(1));
		}
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("getPostDays(int, int, int) - end"); //$NON-NLS-1$
		}
		return set;
	}

	public List retrievePublishByUser(int id, Calendar cal, int maxnumberpost)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("retrievePublishByUser(int, Calendar, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p = JForumExecutionContext
				.getConnection()
				.prepareStatement(
						SystemGlobals
								.getSql("BlogPostModel.selectPublishByUserDateLimit"));
		p.setInt(1, id);
		p.setTimestamp(2, new Timestamp(cal.getTime().getTime()));
		p.setInt(3, maxnumberpost);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("retrievePublishByUser(int, Calendar, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

}
